package cn.org.timespace.easyframe.action.permission;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.org.timespace.easyframe.action.EFBaseAction;
import cn.org.timespace.easyframe.model.permission.EfMenu;
import cn.org.timespace.easyframe.model.permission.EfRoleMenu;
import cn.org.timespace.easyframe.service.permission.EfMenuService;
import cn.org.timespace.easyframe.service.permission.EfRoleMenuService;

/**
 * <p>
 * 角色菜单Action
 * </p>
 * 
 * @version 1.0, 11/02/27
 * @since 1.0
 * @author wubo
 */ 

public class EfRoleMenuAction extends EFBaseAction {

	private static final long serialVersionUID = -3416183313291298070L;

	private static Log log = LogFactory.getLog(EfRoleMenuAction.class);

	private EfMenuService efMenuService;
	private EfRoleMenuService efRoleMenuService;

	public EfRoleMenuAction() {
		super(new EFMenuModel());
	}

	EFMenuModel efMenuModel = (EFMenuModel) this.getModel();

	public String doList() throws Exception {

		// 查询所有的菜单数据
		List<EfMenu> efMenuList = efMenuService.findByCondition(efMenuModel);
		getRequest().setAttribute(VOLIST, efMenuList);

		// 查询该角色目前已经关联的菜单
		String roleId = getRequest().getParameter("roleId");
		List<EfRoleMenu> efRoleMenuList = efRoleMenuService.findByCondition(roleId);
		getRequest().setAttribute("efRoleMenuList", efRoleMenuList);
		getRequest().setAttribute("efRoleMenuListSize", efRoleMenuList.size());

		// 将roleId传递至页面
		getRequest().setAttribute("roleId", roleId);

		return LIST;
	}

	public String doSave() throws Exception {
		String pageMsg = "保存成功";
		
		// 获取页面传过来的用户ID信息
		String roleId = getRequest().getParameter("roleId");
		String menuIds = getRequest().getParameter("menuIds");
		if (log.isInfoEnabled())
			log.info("角色授权[角色：" + roleId + "   选择的菜单：" + menuIds + "]");
		
		try{
			efRoleMenuService.saveRoleMenu(roleId, menuIds);
		}catch (Exception e){
			pageMsg = "保存失败！原因是："+e.getMessage();
		}
		
		getRequest().setAttribute(PAGEMSG,pageMsg);
		return doList();
	}

	public EfMenuService getEfMenuService() {
		return efMenuService;
	}

	public void setEfMenuService(EfMenuService efMenuService) {
		this.efMenuService = efMenuService;
	}

	public EfRoleMenuService getEfRoleMenuService() {
		return efRoleMenuService;
	}

	public void setEfRoleMenuService(EfRoleMenuService efRoleMenuService) {
		this.efRoleMenuService = efRoleMenuService;
	}

}
